package com.qf.index.gcrdo.ab;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSONArray;
import com.qf.index.TransApi;
import com.qf.index.gcrdo.StringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import static com.qf.index.TransApi.aLiEnToCnTransBySingleYouDao;

/**
 * @Author: 葛成瑞
 * @Date: 2025/4/22 13:36
 */
@Slf4j
public class DoTransAbRecord {

    @SneakyThrows
    public static void main(String[] args) {

        // 此程序是用于遍历unified_search_summary_0406 表只要字段 cn_discussion cn_results cn_methods cn_background  有值 且翻译没有翻译的则记录下来

        // 遍历unified_search_summary_0406 表
        String table = "unified_search_no_trans_ab";
        String saveTable = "unified_search_no_trans_ab";
        Db use = Db.use();
        Integer maxId = use.query("select max(id) as maxid from " + table).get(0).getInt("maxid");
        System.out.println(maxId);
        Integer minId = 0;
        Integer step = 10000;
        for (int i = minId; i < maxId; i += step) {
            System.out.println("页码：" + i + " 到 " + (i + step) + " 开始查询");
            String sql = "select id,cn_discussion,cn_results,cn_methods,cn_background from " + table + " where id > " + i + " and id <= " + (i + step) + " ";
            List<Entity> query = use.query(sql);
//            query.parallelStream().forEach(entity -> {
            query.forEach(entity -> {
                Integer id = entity.getInt("id");
                String cn_discussion = entity.getStr("cn_discussion");
                String cn_results = entity.getStr("cn_results");
                String cn_methods = entity.getStr("cn_methods");
                String cn_background = entity.getStr("cn_background");
                boolean cn_discussionisIns = false;
                boolean cn_resultsisIns = false;
                boolean cn_methodsisIns = false;
                boolean cn_backgroundisIns = false;
                String pre = "ĺļ";
                if (StringUtil.isNotNull(cn_discussion)) {
                    List<AbEntity> abEntities = JSONArray.parseArray(cn_discussion, AbEntity.class);
                    for (AbEntity abEntity : abEntities) {
                        String cn = abEntity.getCn();
                        String en = abEntity.getEn();
                        boolean bcn = checkIsTrans(cn);
                        boolean ben = checkIsTrans(en);
                        if (bcn && !ben) {
                            String trans = trans(cn, true);
                            if (StringUtil.isNotNull(trans)) {
                                abEntity.setEn(pre + trans);
                            }
                        } else if (!bcn && ben) {
                            String trans = trans(en, false);
                            if (StringUtil.isNotNull(trans)) {
                                abEntity.setCn(pre + trans);
                            }
                        }
                    }
                    String jsonString = JSONArray.toJSONString(abEntities);
                    cn_discussion = jsonString;
                }
                if (StringUtil.isNotNull(cn_results)) {
                    List<AbEntity> abEntities = JSONArray.parseArray(cn_results, AbEntity.class);
                    for (AbEntity abEntity : abEntities) {
                        String cn = abEntity.getCn();
                        String en = abEntity.getEn();
                        boolean bcn = checkIsTrans(cn);
                        boolean ben = checkIsTrans(en);
                        if (bcn && !ben) {
                            String trans = trans(cn, true);
                            if (StringUtil.isNotNull(trans)) {
                                abEntity.setEn(pre + trans);
                            }
                        } else if (!bcn && ben) {
                            String trans = trans(en, false);
                            if (StringUtil.isNotNull(trans)) {
                                abEntity.setCn(pre + trans);
                            }
                        }
                    }
                    String jsonString = JSONArray.toJSONString(abEntities);
                    cn_results = jsonString;
                }
                if (StringUtil.isNotNull(cn_methods)) {
                    List<AbEntity> abEntities = JSONArray.parseArray(cn_methods, AbEntity.class);
                    for (AbEntity abEntity : abEntities) {
                        String cn = abEntity.getCn();
                        String en = abEntity.getEn();
                        boolean bcn = checkIsTrans(cn);
                        boolean ben = checkIsTrans(en);
                        if (bcn && !ben) {
                            String trans = trans(cn, true);
                            if (StringUtil.isNotNull(trans)) {
                                abEntity.setEn(pre + trans);
                            }
                        } else if (!bcn && ben) {
                            String trans = trans(en, false);
                            if (StringUtil.isNotNull(trans)) {
                                abEntity.setCn(pre + trans);
                            }
                        }
                    }
                    String jsonString = JSONArray.toJSONString(abEntities);
                    cn_methods = jsonString;
                }
                if (StringUtil.isNotNull(cn_background)) {
                    List<AbEntity> abEntities = JSONArray.parseArray(cn_background, AbEntity.class);
                    for (AbEntity abEntity : abEntities) {
                        String cn = abEntity.getCn();
                        String en = abEntity.getEn();
                        boolean bcn = checkIsTrans(cn);
                        boolean ben = checkIsTrans(en);
                        if (bcn && !ben) {
                            String trans = trans(cn, true);
                            if (StringUtil.isNotNull(trans)) {
                                abEntity.setEn(pre + trans);
                            }
                        } else if (!bcn && ben) {
                            String trans = trans(en, false);
                            if (StringUtil.isNotNull(trans)) {
                                abEntity.setCn(pre + trans);
                            }
                        }
                    }
                    String jsonString = JSONArray.toJSONString(abEntities);
                    cn_background = jsonString;
                }
                entity.set("cn_discussion", cn_discussion);
                entity.set("cn_results", cn_results);
                entity.set("cn_methods", cn_methods);
                entity.set("cn_background", cn_background);
                entity.setTableName(saveTable);
                try {
                    System.out.println("已经更行+" + id);
                    use.insertOrUpdate(entity, "id");
                } catch (Exception e) {
                    e.printStackTrace();
                }


            });
        }

    }

    public static boolean checkIsTrans(String en) {
        if (StringUtil.isNull(en)) {
            return false;
        }
        if (StringUtil.isNotNull(en)) {
            String ĺļ = en.replace("ĺļ", "");
            if (StringUtil.isNull(ĺļ)) {
                return false;
            }
        }
        return true;

    }

    public static String trans(String str, boolean isCn) {
        String res = "";
        if (!isCn) {
            String s = TransApi.youDaoEnToCnTrans(str);
            if (StringUtil.isNotNull(s) && !s.contains("502 Bad Gateway")) {
                res = s;
            } else {
                String s3 = aLiEnToCnTransBySingleYouDao(str, 180, isCn);
                if (StringUtil.isNotNull(s3) &&!s3.contains("502 Bad Gateway")) {
                    res = s3;
                }
            }
        } else {
            String s = TransApi.youDaoCnToEnTrans(str);
            if (StringUtil.isNotNull(s) && !s.contains("502 Bad Gateway")) {
                res = s;
            } else {
                String s3 = aLiEnToCnTransBySingleYouDao(str, 180, isCn);
                if (StringUtil.isNotNull(s3) &&!s3.contains("502 Bad Gateway")) {
                    res = s3;
                }
            }
        }
        return res;
    }
}
